#include "SDLutil.h"

SDL_Surface *load_image(string filename) {
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP(filename.c_str());
    if(loadedImage != NULL) {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
    }
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, NULL, destination, &offset);
}

void apply_surface_from(int x, int y, SDL_Surface *source, SDL_Rect *sourceRect, SDL_Surface *destination) {
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	SDL_BlitSurface(source, sourceRect, destination, &offset);
}

void draw_pixel(SDL_Surface *screen, Uint32 x, Uint32 y, Uint32 color) {
	Uint16 *bufp;
	SDL_LockSurface(screen);
	bufp = (Uint16 *)screen->pixels + y * screen->pitch/2 + x;
	*bufp = color;
	SDL_UnlockSurface(screen);
}

void draw_line(SDL_Surface *screen, Uint32 x, Uint32 y, Uint32 h, Uint32 color) {
	for(int i = y; i < (y + h); i++) draw_pixel(screen, x, i, color);
}
